(function()
{
      var down = CKEDITOR.plugins.getPath('simulateFormControls') + 'images/radiobutton-down.png';
      var up = CKEDITOR.plugins.getPath('simulateFormControls') + 'images/radiobutton-up.png';
      var radioButtonData = new Object();
            
      function radioButtonDialog( editor ){
        return {
          title : 'Properties Radio Button',
          resizable : CKEDITOR.DIALOG_RESIZE_BOTH,
          minWidth : 400,
          minHeight : 400,
          onShow : function(){
            delete this.radioButton;

      			var element = this.getParentEditor().getSelection().getSelectedElement();
      			if ( element && element.getName() == "img" && element.getAttribute( 'type' ) == "radioButton" )
      			{
      				this.radioButton = element;
      				this.setupContent( element );
      			}  
          },
          onOk : function(){
            var editor,
      				element = this.radioButton,
      				isInsertMode = !element;
      
            this.commitContent( { element : element } );
      
      			if ( isInsertMode )
      			{
      				editor = this.getParentEditor();
      				element = editor.document.createElement( 'img' );
      				element.setAttribute( 'type', 'radioButton' );
      				element.setAttribute( 'class', 'radioButton' );
      				element.setAttribute( 'style', 'cursor: pointer;' );
      				element.setAttribute( 'width', '16' );
      				element.setAttribute( 'height', '16' );
      				
              if(radioButtonData.name){
      				  element.setAttribute( 'radioName', radioButtonData.name);
      				}
      				
              if(radioButtonData.checked){
      				  element.setAttribute( 'src', up);
      				}else{
                element.setAttribute( 'src', down);
              }
      				
              element.setAttribute( 'onClick', 'var elem = document.getElementsByTagName(\'img\'); for(i = 0; i < elem.length; i++){if(elem[i].getAttribute(\'radioName\') == \''+radioButtonData.name+'\'){elem[i].src = \'' + down + '\'}} this.src = \'' + up + '\'');
      				
              if(radioButtonData.value){
                element.setAttribute('value', radioButtonData.value);
              }
      				
              delete radioButtonData.name;
      				delete radioButtonData.checked;
      				delete radioButtonData.value;
      			}
      
      			if ( isInsertMode )
      				editor.insertElement( element );
          },
          contents : [
            {
              id : 'tab1',
              label : 'Tab1',
              title : 'Properties Radio Button',
              elements : [
                {
                  type : 'text',
                  label : 'Name<br />',
                  id : 'nameRadioButton',
                  'default' : '',
                  setup : function( element )
      						{
                    this.setValue(element.getAttribute('radioName') || ''); 
      						},
      						commit : function( data )
      						{ 
      							radioButtonData.name = this.getValue();
      						}
                },
                {
                  type : 'text',
                  label : 'Value<br />',
                  id : 'valueRadioButton',
                  'default' : '',
      						setup : function( element )
      						{
      						  this.setValue(element.getAttribute('value') || '');	
      						},
      						commit : function( data )
      						{
      						  radioButtonData.value = this.getValue();	
      						}
                },
                {
                  type: 'checkbox',
                  label: 'Selected',
                  id: 'selectedRadioButton',
      						setup : function( element )
      						{
      						  if(element.getAttribute('src') != down){
                      this.setValue('checked');  
                    }
      						},
      						commit : function( data )
      						{
      						  radioButtonData.checked = this.getValue();	
      						}
                }]
            }]
        };
      }
      
    	CKEDITOR.dialog.add( 'simulatedRadioButton', function( editor )
  		{
  			return radioButtonDialog( editor);
  		} );
})();